# Copyright (c) 2018 Presto Labs Pte. Ltd.
# Author: inkyu

import datetime
import logging

from absl import app, flags
from tornado.ioloop import IOLoop

import coin.flow.subscriber as flow_subscriber
from coin.flow.simple_queue import SimpleQueue
from coin.strategy.hk_hard_arb.util import load_clean_json

from coin.strategy.cheese.base import BaseStrategy

FLAGS = flags.FLAGS

flags.DEFINE_string('exchanges', 'Exchanges', '')

flags.DEFINE_string('name', '', 'unhedge_bithumb_a')

flags.DEFINE_string('revision', '', '20181201')

flags.DEFINE_string('og_config', '', '')

flags.DEFINE_string('config_filename', '', '')

flags.DEFINE_integer('exit_after_min', 1440, '')

flags.DEFINE_boolean('dry_run', True, '')


def get_param_set(exchanges, og_config, name, revision, config_filename):
  exchange_0, exchange_1 = exchanges.split(',')
  og_config = og_config
  config = load_clean_json(config_filename)
  return {
      'exchange_0': exchange_0,
      'exchange_1': exchange_1,
      'og_config': og_config,
      'config': config,
      'name': name,
      'revision': revision,
  }


def main(argv):
  strategy_params = get_param_set(exchanges=FLAGS.exchanges,
                                  og_config=FLAGS.og_config,
                                  name=FLAGS.name,
                                  revision=FLAGS.revision,
                                  config_filename=FLAGS.config_filename)

  queue = SimpleQueue('cheese_hard_arb')
  flow_sub = flow_subscriber.from_queue(queue)
  ioloop = IOLoop.current()
  strategy = BaseStrategy(strategy_params)
  strategy.subscribe_feeds()
  strategy.subscribe_live_env(ioloop, queue, flow_sub)
  strategy.start(ioloop, queue, flow_sub)

  exit_after = datetime.timedelta(minutes=FLAGS.exit_after_min)
  ioloop.add_timeout(exit_after, ioloop.stop)
  ioloop.start()


if __name__ == '__main__':
  logging.basicConfig(level='DEBUG', format='%(levelname)8s %(asctime)s %(name)s] %(message)s')
  app.run(main)
